//
//  PVDDetailJobInfoView.swift
//  GreenDeal
//
//  Created by Moennig Q on 26/12/2022.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
import RxGesture
class JobDetailTopView: CustomBackView {
    private lazy var disposeBag = DisposeBag()
    var addressLabelLongPressGestureClosure: (()->Void)?
    //
    let outsizeLeftRightMargin: CGFloat = 12
    var realSize: CGSize  {
        var size = CGSize(width: 0, height: 0)
        let maxWidth = (APPConfig.WidthForPortrait - btnsInternal * 2 - (Constant.AllOutMargin + outsizeLeftRightMargin) * 2) / 3
        if maxWidth > 150 {
            size = CGSize(width: 150, height: 90)
        }else{
            size = CGSize(width: maxWidth, height: 90)
        }
        return size
    }
    let btnsInternal: CGFloat = 8
    
    let propertyTypeImageView = PropertyTypeImageButton()
    let installationTypeImageView = PropertyTypeImageButton()
    let gridConnectedImageView = PropertyTypeImageButton()
    let addressLabel = ContentLabel()
    let editAddressBtn = ViewFactory.shared.generateMoreImageView()
    let phoneBtn = UIButton()
    let emailBtn = UIButton()
    let mapNaviBtn = UIButton()
    override func setUpView() {
        //propertyType
        self.addSubview(propertyTypeImageView)
        propertyTypeImageView.snp.makeConstraints { make in
            make.top.equalTo(Constant.AllOutMargin)
            make.left.equalTo(Constant.AllOutMargin)
            make.size.equalTo(realSize)
        }
        //installationType
        self.addSubview(installationTypeImageView)
        installationTypeImageView.snp.makeConstraints { make in
            make.centerY.equalTo(propertyTypeImageView)
            make.left.equalTo(propertyTypeImageView.snp.right).offset(btnsInternal)
            make.size.equalTo(propertyTypeImageView)
        }
        //gridConnected
        self.addSubview(gridConnectedImageView)
        gridConnectedImageView.snp.makeConstraints { make in
            make.centerY.equalTo(propertyTypeImageView)
            make.left.equalTo(installationTypeImageView.snp.right).offset(btnsInternal)
            make.size.equalTo(propertyTypeImageView)
        }
        //Address
        addressLabel.numberOfLines = 0
        self.addSubview(addressLabel)
        addressLabel.snp.makeConstraints { make in
            make.top.equalTo(propertyTypeImageView.snp.bottom).offset(16)
            make.left.equalTo(propertyTypeImageView)
            make.height.greaterThanOrEqualTo(0)
        }
        //edit address btn
        self.addSubview(editAddressBtn)
        editAddressBtn.snp.makeConstraints { make in
            make.centerY.equalTo(addressLabel)
            make.left.equalTo(addressLabel.snp.right)
            make.right.equalTo(-Constant.AllOutMargin)
            make.height.width.equalTo(24)
        }
        //phone btn
        phoneBtn.setImage(R.image.jobdetail_phone(), for: UIControl.State.normal)
        self.addSubview(phoneBtn)
        phoneBtn.snp.makeConstraints { make in
            make.left.equalTo(propertyTypeImageView)
            make.top.equalTo(addressLabel.snp.bottom).offset(12)
            make.size.equalTo(CGSize(width: 40, height: 40))
            make.bottom.equalTo(-Constant.AllOutMargin)
        }
        //email btn
        emailBtn.setImage(R.image.jobdetail_email(), for: UIControl.State.normal)
        self.addSubview(emailBtn)
        emailBtn.snp.makeConstraints { make in
            make.left.equalTo(phoneBtn.snp.right).offset(16)
            make.size.top.equalTo(phoneBtn)
        }
        //map navi btn
        mapNaviBtn.setImage(R.image.jobdetal_map_navi(), for: UIControl.State.normal)
        self.addSubview(mapNaviBtn)
        mapNaviBtn.snp.makeConstraints { make in
            make.left.equalTo(emailBtn.snp.right).offset(16)
            make.size.top.equalTo(phoneBtn)
        }
        //
        self.bindData()
    }
    @objc func addressLabelLongPressGesture(){
        self.addressLabelLongPressGestureClosure?()
    }
    private func bindData(){
        //
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addressLabelLongPressGesture))
        addressLabel.addGestureRecognizer(longPressGesture)
        addressLabel.isUserInteractionEnabled = true
        //special address or installation address
        JobDetailModelHelper.shared.install_address.map({$0.final_install_address}).subscribe { [weak self] (data) in
            self?.addressLabel.text = data
        }.disposed(by: self.disposeBag)
        //installation address
        JobDetailModelHelper.shared.install_address.map({$0.property_type }).subscribe(onNext: { [weak self] (propertyType) in
            guard let strongSelf = self else {return}
            switch propertyType {
            case PropertyType.residential.rawValue:
                strongSelf.propertyTypeImageView.setDataInfo(title: PropertyType.residential.rawValue,imageName: "jobdetail_propertytype_residential")
            case PropertyType.commercial.rawValue:
                strongSelf.propertyTypeImageView.setDataInfo(title: PropertyType.commercial.rawValue,imageName: "jobdetail_propertytype_commercial")
            case PropertyType.school.rawValue:
                strongSelf.propertyTypeImageView.setDataInfo(title: PropertyType.school.rawValue,imageName: "jobdetail_propertytype_school")
            default:
                break
            }
            //处理位置变化
            strongSelf.dealTypeImageViewUI()
        }).disposed(by: self.disposeBag)
        //connnect type
        JobDetailModelHelper.shared.system_detail.map({$0.connected_type}).subscribe(onNext: { [weak self] (gridConnected) in
            guard let strongSelf = self else {return}
            switch gridConnected {
                case GridConnectedValue.onGrid.rawValue:
                strongSelf.gridConnectedImageView.setDataInfo(title: GridConnectedName.onGrid.rawValue,imageName: "jobdetail_gridconnected_on")
                case GridConnectedValue.offGrid.rawValue:
                    strongSelf.gridConnectedImageView.setDataInfo(title: GridConnectedName.offGrid.rawValue,imageName: "jobdetail_gridconnected_off")
                default:
                    break
            }
            //处理位置变化
            strongSelf.dealTypeImageViewUI()
        }).disposed(by: self.disposeBag)
        //installation_type
        JobDetailModelHelper.shared.system_detail.map({$0.installation_type}).subscribe(onNext: { [weak self] (installationType) in
            guard let strongSelf = self else {return}
            switch installationType {
            case InstallationTypeValue.New.rawValue:
                strongSelf.installationTypeImageView.setDataInfo(title: InstallationTypeName.New.rawValue,imageName: "jobdetail_installationtype_new")
            case InstallationTypeValue.Replacement.rawValue:
                strongSelf.installationTypeImageView.setDataInfo(title: InstallationTypeName.Replacement.rawValue,imageName: "jobdetail_installationtype_replacement")
            case InstallationTypeValue.Extension.rawValue:
                strongSelf.installationTypeImageView.setDataInfo(title: InstallationTypeName.Extension.rawValue,imageName: "jobdetail_installationtype_extension")
            case InstallationTypeValue.Additional.rawValue:
                strongSelf.installationTypeImageView.setDataInfo(title: InstallationTypeName.Additional.rawValue,imageName: "jobdetail_installationtype_additional")
            default:
                break
            }
            //处理位置变化
            strongSelf.dealTypeImageViewUI()
        }).disposed(by: self.disposeBag)
    }
    //MARK: - 处理property type,installation type, connected_type三个图标的显示
    private func dealTypeImageViewUI(){
        let propertyTypeEmptyFlag = JobDetailModelHelper.shared.jobDetailModel.install_address.property_type == "" ? true : false
        let installationTypeEmptyFlag = JobDetailModelHelper.shared.jobDetailModel.system_detail.installation_type == "" ? true : false
        //
        propertyTypeImageView.isHidden =  (propertyTypeEmptyFlag ? true : false)
        propertyTypeImageView.isUserInteractionEnabled =  (propertyTypeEmptyFlag ? false : true)
        //
        installationTypeImageView.isHidden = (installationTypeEmptyFlag ? true : false)
        installationTypeImageView.isUserInteractionEnabled = (installationTypeEmptyFlag ? false : true)
        
        if propertyTypeEmptyFlag {//空值
            if installationTypeEmptyFlag {//空值
                //grid connect肯定有值,前边两个为空，grid connected就放在最前边
                gridConnectedImageView.snp.remakeConstraints { make in
                    make.center.size.equalTo(propertyTypeImageView)
                }
            }else{//有值
                installationTypeImageView.snp.remakeConstraints { make in
                    make.center.size.equalTo(propertyTypeImageView)
                }
                gridConnectedImageView.snp.remakeConstraints { make in
                    make.centerY.size.equalTo(propertyTypeImageView)
                    make.left.equalTo(installationTypeImageView.snp.right).offset(btnsInternal)
                }
            }
        }else{//有值
            if installationTypeEmptyFlag {//空值
                gridConnectedImageView.snp.remakeConstraints { make in
                    make.centerY.size.equalTo(propertyTypeImageView)
                    make.left.equalTo(propertyTypeImageView.snp.right).offset(btnsInternal)
                }
            }else{
                installationTypeImageView.snp.remakeConstraints { make in
                    make.centerY.size.equalTo(propertyTypeImageView)
                    make.left.equalTo(propertyTypeImageView.snp.right).offset(btnsInternal)
                }
                gridConnectedImageView.snp.remakeConstraints { make in
                    make.centerY.size.equalTo(propertyTypeImageView)
                    make.left.equalTo(installationTypeImageView.snp.right).offset(btnsInternal)
                }
            }
        }
    }
}
